package com.zeros.myself.practice_delete.practice7.sample;

import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.zeros.myself.R;

/**
 * @author zxx on 2020/10/13
 */
public class Sample06KeyframeLayout extends RelativeLayout {

    public Sample06KeyframeLayout(Context context) {
        super(context);
    }

    public Sample06KeyframeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Sample06KeyframeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    Sample06KeyframeView view;
    Button animateBt;

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        view = (Sample06KeyframeView) findViewById(R.id.objectAnimatorView);
        animateBt = (Button) findViewById(R.id.animateBt);

        animateBt.setOnClickListener(v -> {
            // 使用 Keyframe.ofFloat() 来为 view 的 progress 属性创建关键帧
            // 初始帧：progress 为 0
            // 时间进行到一般：progress 为 100
            // 结束帧：progress 回落到 80
            // 使用 PropertyValuesHolder.ofKeyframe() 来把关键帧拼接成一个完整的属性动画方案
            // 使用 ObjectAnimator.ofPropertyValuesHolder() 来创建动画

            Keyframe keyframe1 = Keyframe.ofFloat(0, 0);    // 开始：progress 为 0
            Keyframe keyframe2 = Keyframe.ofFloat(0.5f, 100);   // 进行到一半是，progres 为 100
            Keyframe keyframe3 = Keyframe.ofFloat(1, 80);   // 结束时倒回到 80
            PropertyValuesHolder holder = PropertyValuesHolder.ofKeyframe("progress", keyframe1, keyframe2, keyframe3);

            ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, holder);
            animator.setDuration(2000);
            animator.setInterpolator(new FastOutSlowInInterpolator());
            animator.start();
        });
    }
}
